<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="${pageContext.request.contextPath }/js/kindeditor-4.1.10/themes/default/default.css" type="text/css" rel="stylesheet">
<script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath }/js/kindeditor-4.1.10/kindeditor-all-min.js"></script>
<script type="text/javascript" charset="utf-8" src="${pageContext.request.contextPath }/js/kindeditor-4.1.10/lang/zh_CN.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.8.3.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/ajaxfileupload.js"></script>
<link id="easyuiTheme" rel="stylesheet" type="text/css"	href="${pageContext.request.contextPath }/js/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/js/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath }/css/default.css">
<script type="text/javascript" src="${pageContext.request.contextPath }/js/easyui/jquery.easyui.min.js"></script>
<script	src="${pageContext.request.contextPath }/js/easyui/locale/easyui-lang-zh_CN.js"	type="text/javascript"></script>
<script src="${pageContext.request.contextPath }/js/outOfBounds.js" type="text/javascript"></script>
<script src="${pageContext.request.contextPath }/js/common.js" type="text/javascript"></script>
</head>

<body>
<div class="easyui-layout" data-options="fit:true">
	
	<div data-options="region:'center'" style="padding:5px">
	<table class="easyui-datagrid" id="articleList"
       data-options="singleSelect:false,collapsible:true,pagination:true,url:'${pageContext.request.contextPath }/admin/article/list.do',method:'get',pageSize:30,toolbar:toolbar,fitColumns:true">
    <input id="ss" class="easyui-searchbox" style="width: 300px" data-options="searcher:doSearchArticle,prompt:'请输入要查找的id',menu:'#mm'"></input>
    <thead>
        <tr>
        	<th data-options="field:'ck',checkbox:true"></th>
            <th data-options="field:'id',width:100,align:'center'">id</th>
           <th data-options="field:'title',width:300,align:'center'">标题</th>
            <th data-options="field:'name',width:80,align:'center'">分类</th>
            <th data-options="field:'editor',width:80,align:'center'">发布人</th>
            <th data-options="field:'keywords',width:150,align:'center'">关键字</th>
            <th data-options="field:'summary',width:300,align:'center'">概述</th>
            <!-- <th data-options="field:'status',width:60,align:'center',formatter:HONGXIN.formatStatus">是否可用</th> -->
            <th data-options="field:'updateTime',width:130,align:'center',formatter:HONGXIN.formatDateTime">更新日期</th>
            <th data-options="field:'createTime',width:130,align:'center',formatter:HONGXIN.formatDateTime">发布日期</th>
        </tr>
    </thead>
	</table>
	</div>
</div>
</body>
<div id="articleEditWindow" class="easyui-window" title="编辑文章" data-options="modal:true,closed:true,iconCls:'icon-save',href:'${pageContext.request.contextPath }/admin/article/article_edit.do'" style="width:1000px;height:500px;padding:10px;" ></div>

	<div id="mm" style="width: 120px">
		<div data-options="name:'id'">id</div>
	</div>
<script>

	function doSearchArticle(value, name) {
		var options = $('#articleList').datagrid('getPager').data('pagination').options;
		if (value == '' || value.length == 0){
			//参数options.pageNumber, options.pageSize
			//发送post强求
			var params = {"page":options.pageNumber,"rows":options.pageSize};
			$.post("${pageContext.request.contextPath }/article/list",params, function(oData) {
				$('#articleList').datagrid('loadData', {
					"total" : oData.total,
					"rows" : oData.rows
				});
			});
		}else{
			//参数options.pageNumber, options.pageSize
			//发送post强求
			var params = {"page":options.pageNumber,"rows":options.pageSize,"name":name,"value":value};
			$.post("${pageContext.request.contextPath }/article/findbyproperty",params, function(oData) {
				if(oData.total == 0){
					$.messager.alert('提示','没有查询到相关内容!');
				}else{
				$('#articleList').datagrid('loadData', {
					"total" : oData.total,
					"rows" : oData.rows
				});
					
				}
			});

		}
	};
			
	function upload(){
	    $.ajaxFileUpload({
	        url:'file/upload',//用于文件上传的服务器端请求地址
	        secureuri:true,//是否启用安全提交，一般设置为false
	        fileElementId:'uploadFile',//文件上传控件的id
	        dataType:'text',//服务器返回的数据类型
	        //success: function (data,status){
	        success: function (data,status){
						
	        	$('.datagrid-mask-msg').remove(); 
            	$('.datagrid-mask').remove();
				//手动截取字符串,转换为json对象
				var jsonstring = data.substring(data.indexOf('{'),data.lastIndexOf('}') + 1);
				var jsondata = jQuery.parseJSON(jsonstring);
				
				if(jsondata.error == '0'){
					$('#videoUrl').val(jsondata.url);
					$.messager.alert('提示','视频上传成功!');
				}else{
					$.messager.alert('提示','视频上传失败!');
				}
				
	
	        },
	        error: function (data,status,e){
	            alert("服务中断或连接超时导致通信失败！");
	        }
	    }
	    )
	    return false;
	}

    function getSelectionsIds(){
    	var articleList = $("#articleList");
    	var sels = articleList.datagrid("getSelections");
    	var ids = [];
    	for(var i in sels){
    		ids.push(sels[i].id);
    	}
    	ids = ids.join(",");
    	return ids;
    }
    
    var toolbar = [{
        text:'编辑',
        iconCls:'icon-edit',
        handler:function(){
        	var ids = getSelectionsIds();
        	if(ids.length == 0){
        		$.messager.alert('提示','必须选择一则文章才能编辑!');
        		return ;
        	}
        	if(ids.indexOf(',') > 0){
        		$.messager.alert('提示','只能选择一则文章!');
        		return ;
        	}
        	
        	$("#articleEditWindow").window({
            		onLoad :function(){
            			//回显基础数据
            			//获取选中行的数据
            			var data = $("#articleList").datagrid("getSelections")[0];
            			
            			$("#articleEditForm").form("load",data);
            			HONGXIN.init({
            				//回显图片
            				"pics" : data.imgUrl,
            				//回显文章分类
            				"cid" : data.name,
            				//"name" : date.name,
   			
            			});
            			var articleEditEditor ;
            			$(function(){
            				//实例化编辑器
            				articleEditEditor = HONGXIN.createEditor("#articleEditForm [name=content]");
            			});
            			
            			// 加载文章描述
            			$.getJSON('articleDetail.do?id='+data.id,function(_data){
            				articleEditEditor.html(_data.content);
            				/* if(_data.status == 200){
            					console.log(_data)
            					articleEditEditor.html(_data.data);
            				} */
            			});
            			
            		}
            		
            		}
            	).window("open");
        	
        	
        	
        }
    },{
        text:'删除',
        iconCls:'icon-cancel',
        handler:function(){
        	var ids = getSelectionsIds();
        	if(ids.length == 0){
        		$.messager.alert('提示','未选中文章!');
        		return ;
        	}
        	$.messager.confirm('确认','确定删除ID为 '+ids+' 的文章吗？',function(r){
        	    if (r){
        	    	var params = {"ids":ids};
                	$.post("${pageContext.request.contextPath }/admin/article/delete.do",params, function(data){
            			if(data.status == 200){
            				$.messager.alert('提示',data.data,undefined,function(){
            					$("#articleList").datagrid("reload");
            				});
            			}else{
            				$.messager.alert('提示',data.data,undefined,function(){
            					$("#articleList").datagrid("reload");
            				});
            			
            			}
            		});
        	    }
        	});
        }
    }];
    
    


</script>